package com.bcbim.operation_maintenance.space.service;

import com.bcbim.common.sys.Constant;
import com.bcbim.operation_maintenance.space.mapper.SpaceStatisticsMapper;
import com.bcbim.operation_maintenance_interface.space.dto.SpaceRisingSunStatisticsDTO;
import com.bcbim.operation_maintenance_interface.space.dto.SpaceStatisticsDTO;
import com.bcbim.operation_maintenance_interface.space.service.SpaceStatisticsService;
import com.bcbim.operation_maintenance_interface.space.vo.SpaceRisingSunStatisticsVO;
import com.bcbim.operation_maintenance_interface.space.vo.SpaceStatisticsVO;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @version V 1.0.0
 * @Description：
 * @Author: haomeijie
 * @Date: 2022/4/20 10:41
 **/
@Service(timeout = 60 * 1000, group = "spaceStatistics", version = "1.0.0")
public class SpaceStatisticsServiceImpl implements SpaceStatisticsService {

    @Autowired
    private SpaceStatisticsMapper spaceStatisticsMapper;

    @Override
    public Map<String, Object> getStackingDiagram(SpaceStatisticsDTO statisticsDTO) {
        //查询数据
        List<SpaceStatisticsVO> diagram = spaceStatisticsMapper.getStackingDiagram(statisticsDTO);
        //整理数据
        List<String> yList = new ArrayList();
        //筛选y轴
        for (int i = 0; i < diagram.size(); i++) {
            String yAxis = diagram.get(i).getYAxis();
            if (!yList.contains(yAxis)) {
                yList.add(diagram.get(i).getYAxis());
            }
        }
        //整理分组数据
        List<String> group = new ArrayList();
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (SpaceStatisticsVO statisticsVO : diagram) {
            String groupName = statisticsVO.getGroupName();
            String name = groupName;
            if (!group.contains(groupName)) {
                //获取同一分组的所有数据
                List<SpaceStatisticsVO> groupList = diagram.stream().filter(s -> StringUtils.isNotEmpty(s.getGroupName()) && s.getGroupName().equals(name)).collect(Collectors.toList());
                double[] values = new double[yList.size()];
                for (int i = 0; i < yList.size(); i++) {
                    double value = 0.00;
                    for (SpaceStatisticsVO spaceStatisticsVO : groupList) {
                        if (yList.get(i).equals(spaceStatisticsVO.getYAxis())) {
                            if (statisticsDTO.getXAxis().equals(Constant.SPACE_COUNT)) {
                                value = spaceStatisticsVO.getSpaceCount();
                            } else {
                                value = spaceStatisticsVO.getArea();
                            }
                        }
                    }
                    values[i] = value;
                }
                //顶级类别手动翻译
                Map<String, Object> map = new HashMap<>(16);
                if (statisticsDTO.getGroup().equals("3") && groupName.equals("0")) {
                    groupName = "无";
                } else {
                    if (statisticsDTO.getGroup().equals("3") && groupName.equals("1")) {
                        groupName = "可用区域";
                    }
                    if (statisticsDTO.getGroup().equals("3") && groupName.equals("2")) {
                        groupName = "服务区域";
                    }
                    if (statisticsDTO.getGroup().equals("3") && groupName.equals("3")) {
                        groupName = "垂直区域";
                    }
                    if (statisticsDTO.getGroup().equals("3") && groupName.equals("4")) {
                        groupName = "其他区域";
                    }
                }
                map.put("name", groupName);
                map.put("value", values);
                mapList.add(map);
                group.add(statisticsVO.getGroupName());
            }
        }
        Map<String, Object> returnData = new HashMap<>();
        //y轴顶级类别翻译
        for (int i = 0; i < yList.size(); i++) {
            String yAxis = yList.get(i);
            if (statisticsDTO.getYAxis().equals("3") && yAxis.equals("0")) {
                yAxis = "无";
            } else {
                if (statisticsDTO.getYAxis().equals("3") && yAxis.equals("1")) {
                    yAxis = "可用区域";
                }
                if (statisticsDTO.getYAxis().equals("3") && yAxis.equals("2")) {
                    yAxis = "服务区域";
                }
                if (statisticsDTO.getYAxis().equals("3") && yAxis.equals("3")) {
                    yAxis = "垂直区域";
                }
                if (statisticsDTO.getYAxis().equals("3") && yAxis.equals("4")) {
                    yAxis = "其他区域";
                }
            }
            yList.remove(i);
            yList.add(i, yAxis);
        }
        returnData.put("yAxis", yList);
        returnData.put("data", mapList);
        return returnData;
    }

    /**
     * @param spaceRisingSunStatisticsDTO
     * @Description: 空间统计旭日图
     * @Date: 2022/4/21 10:24
     * @Param: [spaceRisingSunStatisticsVO]
     * @return: java.util.List<com.bcbim.model_interface.space.dto.SpaceRisingSunStatisticsDTO>
     * @Author: lihonggang
     */
    @Override
    public List<SpaceRisingSunStatisticsVO> getSunburstIllustration(SpaceRisingSunStatisticsDTO spaceRisingSunStatisticsDTO) {
        List<SpaceRisingSunStatisticsVO> assembleOne = null;
        List<SpaceRisingSunStatisticsVO> listGroupTwo = null;
        List<SpaceRisingSunStatisticsVO> listGroupThree = null;
        List<SpaceRisingSunStatisticsVO> listGroupFour = null;
        //查询一级分组数据
        if (ObjectUtils.isNotEmpty(spaceRisingSunStatisticsDTO) && ObjectUtils.isNotEmpty(spaceRisingSunStatisticsDTO.getGroupOne())) {
            spaceRisingSunStatisticsDTO.setGroup(spaceRisingSunStatisticsDTO.getGroupOne());
            assembleOne = spaceStatisticsMapper.getSunburstIllustration(spaceRisingSunStatisticsDTO);
            //如果分组级别是顶级类别,码值翻译
            if (ObjectUtils.isNotEmpty(assembleOne) && spaceRisingSunStatisticsDTO.getGroupOne().equals("3")) {
                for (SpaceRisingSunStatisticsVO risingSunStatisticsVO : assembleOne) {
                    if (StringUtils.isEmpty(risingSunStatisticsVO.getAreaName())) {
                        risingSunStatisticsVO.setAreaName("无");
                    }
                    if (risingSunStatisticsVO.getAreaName().equals("1")) {
                        risingSunStatisticsVO.setAreaName("可用区域");
                    }
                    if (risingSunStatisticsVO.getAreaName().equals("2")) {
                        risingSunStatisticsVO.setAreaName("服务区域");
                    }
                    if (risingSunStatisticsVO.getAreaName().equals("3")) {
                        risingSunStatisticsVO.setAreaName("垂直区域");
                    }
                    if (risingSunStatisticsVO.getAreaName().equals("4")) {
                        risingSunStatisticsVO.setAreaName("其他区域");
                    }
                }
            }
        }
        //获取上级分组的id
        List<String> listIds = new ArrayList<>();

        //查询二级分组数据
        if (ObjectUtils.isNotEmpty(spaceRisingSunStatisticsDTO) && ObjectUtils.isNotEmpty(spaceRisingSunStatisticsDTO.getGroupTwo())) {
            if (ObjectUtils.isNotEmpty(assembleOne)) {
                for (SpaceRisingSunStatisticsVO risingSunStatisticsVO : assembleOne) {
                    listIds.add(risingSunStatisticsVO.getId());
                }
                spaceRisingSunStatisticsDTO.setIds(listIds);
            }
            //二级分组类型
            spaceRisingSunStatisticsDTO.setGroup(spaceRisingSunStatisticsDTO.getGroupTwo());
            //上级分组类型
            spaceRisingSunStatisticsDTO.setGroupUpper(spaceRisingSunStatisticsDTO.getGroupOne());
            listGroupTwo = spaceStatisticsMapper.getSunburstIllustration(spaceRisingSunStatisticsDTO);
            //如果分组级别是顶级类别,码值翻译
            if (ObjectUtils.isNotEmpty(listGroupTwo) && spaceRisingSunStatisticsDTO.getGroupTwo().equals("3")) {
                for (SpaceRisingSunStatisticsVO risingSunStatisticsVO : listGroupTwo) {
                    if (StringUtils.isEmpty(risingSunStatisticsVO.getAreaName())) {
                        risingSunStatisticsVO.setAreaName("无");
                    }
                    if (risingSunStatisticsVO.getAreaName().equals("1")) {
                        risingSunStatisticsVO.setAreaName("可用区域");
                    }
                    if (risingSunStatisticsVO.getAreaName().equals("2")) {
                        risingSunStatisticsVO.setAreaName("服务区域");
                    }
                    if (risingSunStatisticsVO.getAreaName().equals("3")) {
                        risingSunStatisticsVO.setAreaName("垂直区域");
                    }
                    if (risingSunStatisticsVO.getAreaName().equals("4")) {
                        risingSunStatisticsVO.setAreaName("其他区域");
                    }
                }
            }
        }

        //查询三级分组数据
        if (ObjectUtils.isNotEmpty(spaceRisingSunStatisticsDTO) && ObjectUtils.isNotEmpty(spaceRisingSunStatisticsDTO.getGroupThree())) {
            //获取上级分组的id
            listIds.clear();//清空存放id的集合
            if (ObjectUtils.isNotEmpty(listGroupTwo)) {
                for (SpaceRisingSunStatisticsVO risingSunStatisticsVO : listGroupTwo) {
                    listIds.add(risingSunStatisticsVO.getId());
                }
                spaceRisingSunStatisticsDTO.setIds(listIds);
            }
            //三级分组类型
            spaceRisingSunStatisticsDTO.setGroup(spaceRisingSunStatisticsDTO.getGroupThree());
            //二级分组类型
            spaceRisingSunStatisticsDTO.setGroupUpper(spaceRisingSunStatisticsDTO.getGroupTwo());
            //一级分组类型
            spaceRisingSunStatisticsDTO.setGroupFirst(spaceRisingSunStatisticsDTO.getGroupOne());
            listGroupThree = spaceStatisticsMapper.getSunburstIllustration(spaceRisingSunStatisticsDTO);
            //如果分组级别是顶级类别,码值翻译
            if (ObjectUtils.isNotEmpty(listGroupThree) && spaceRisingSunStatisticsDTO.getGroupThree().equals("3")) {
                for (SpaceRisingSunStatisticsVO risingSunStatisticsVO : listGroupThree) {
                    if (StringUtils.isEmpty(risingSunStatisticsVO.getAreaName())) {
                        risingSunStatisticsVO.setAreaName("无");
                    }
                    if (risingSunStatisticsVO.getAreaName().equals("1")) {
                        risingSunStatisticsVO.setAreaName("可用区域");
                    }
                    if (risingSunStatisticsVO.getAreaName().equals("2")) {
                        risingSunStatisticsVO.setAreaName("服务区域");
                    }
                    if (risingSunStatisticsVO.getAreaName().equals("3")) {
                        risingSunStatisticsVO.setAreaName("垂直区域");
                    }
                    if (risingSunStatisticsVO.getAreaName().equals("4")) {
                        risingSunStatisticsVO.setAreaName("其他区域");
                    }
                }
            }
        }

        //四级分组
        if (ObjectUtils.isNotEmpty(spaceRisingSunStatisticsDTO) && ObjectUtils.isNotEmpty(spaceRisingSunStatisticsDTO.getGroupFour())) {
            //获取上级分组的id
            listIds.clear();//清空存放id的集合
            if (ObjectUtils.isNotEmpty(listGroupThree)) {
                for (SpaceRisingSunStatisticsVO risingSunStatisticsVO : listGroupThree) {
                    listIds.add(risingSunStatisticsVO.getId());
                }
                spaceRisingSunStatisticsDTO.setIds(listIds);
            }
            //四级分组类型
            spaceRisingSunStatisticsDTO.setGroup(spaceRisingSunStatisticsDTO.getGroupFour());
            //上级分组类型
            spaceRisingSunStatisticsDTO.setGroupUpper(spaceRisingSunStatisticsDTO.getGroupThree());
            //二级分组类型
            spaceRisingSunStatisticsDTO.setGroupFirst(spaceRisingSunStatisticsDTO.getGroupOne());
            //一级分组类型
            spaceRisingSunStatisticsDTO.setGroupSecond(spaceRisingSunStatisticsDTO.getGroupTwo());
            listGroupFour = spaceStatisticsMapper.getSunburstIllustration(spaceRisingSunStatisticsDTO);
            //如果分组级别是顶级类别,码值翻译
            if (ObjectUtils.isNotEmpty(listGroupFour) && spaceRisingSunStatisticsDTO.getGroupFour().equals("3")) {
                for (SpaceRisingSunStatisticsVO risingSunStatisticsVO : listGroupFour) {
                    if (StringUtils.isEmpty(risingSunStatisticsVO.getAreaName())) {
                        risingSunStatisticsVO.setAreaName("无");
                    }
                    if (risingSunStatisticsVO.getAreaName().equals("1")) {
                        risingSunStatisticsVO.setAreaName("可用区域");
                    }
                    if (risingSunStatisticsVO.getAreaName().equals("2")) {
                        risingSunStatisticsVO.setAreaName("服务区域");
                    }
                    if (risingSunStatisticsVO.getAreaName().equals("3")) {
                        risingSunStatisticsVO.setAreaName("垂直区域");
                    }
                    if (risingSunStatisticsVO.getAreaName().equals("4")) {
                        risingSunStatisticsVO.setAreaName("其他区域");
                    }
                }
            }
        }
        //第二级分组组装
        if (ObjectUtils.isNotEmpty(assembleOne) && ObjectUtils.isNotEmpty(listGroupTwo)) {
            assembleOne = assembleSunburstIllustration(assembleOne, listGroupTwo);
        }
        //第三级分组组装
        if (ObjectUtils.isNotEmpty(assembleOne) && ObjectUtils.isNotEmpty(listGroupThree)) {
            for (SpaceRisingSunStatisticsVO sunStatisticsVO : assembleOne) {
                List<SpaceRisingSunStatisticsVO> sunStatistics = assembleSunburstIllustration(sunStatisticsVO.getChildren(), listGroupThree);
                sunStatisticsVO.setChildren(sunStatistics);
            }
        }
        //第四级分组组装
        if (ObjectUtils.isNotEmpty(assembleOne) && ObjectUtils.isNotEmpty(listGroupFour)) {
            for (SpaceRisingSunStatisticsVO one : assembleOne) {
                for (SpaceRisingSunStatisticsVO two : one.getChildren()) {
                    List<SpaceRisingSunStatisticsVO> sunStatistics = assembleSunburstIllustration(two.getChildren(), listGroupFour);
                    two.setChildren(sunStatistics);

                }
            }
        }
        return assembleOne;
    }

    /**
     * @Description: 整理旭日图数据
     * @Date: 2022/4/21 16:43
     * @Param: [list]
     * @return: java.util.List<com.bcbim.model_interface.space.vo.SpaceSunburstStatisticsVO>
     * @Author: lihonggang
     **/
    private List<SpaceRisingSunStatisticsVO> assembleSunburstIllustration(List<SpaceRisingSunStatisticsVO> listUpper, List<SpaceRisingSunStatisticsVO> listNext) {


        //遍历整理数据
        for (SpaceRisingSunStatisticsVO spaceRisingSunStatisticsVO : listUpper) {
            //创建空间统计旭日图返回对象
            List<SpaceRisingSunStatisticsVO> child = Lists.newArrayList();
            for (SpaceRisingSunStatisticsVO risingSunStatisticsVO : listNext) {
                if (ObjectUtils.isEmpty(risingSunStatisticsVO.getFirstId()) && ObjectUtils.isEmpty(risingSunStatisticsVO.getSecondId())) {
                    if (spaceRisingSunStatisticsVO.getId().equals(risingSunStatisticsVO.getUpperId())) {
                        child.add(risingSunStatisticsVO);
                    }
                } else if (ObjectUtils.isNotEmpty(risingSunStatisticsVO.getFirstId()) && ObjectUtils.isEmpty(risingSunStatisticsVO.getSecondId())) {
                    if (spaceRisingSunStatisticsVO.getId().equals(risingSunStatisticsVO.getUpperId()) && spaceRisingSunStatisticsVO.getUpperId().equals(risingSunStatisticsVO.getFirstId())) {
                        child.add(risingSunStatisticsVO);
                    }
                } else if (ObjectUtils.isNotEmpty(risingSunStatisticsVO.getFirstId()) && ObjectUtils.isNotEmpty(risingSunStatisticsVO.getSecondId())) {
                    if (spaceRisingSunStatisticsVO.getId().equals(risingSunStatisticsVO.getUpperId()) && spaceRisingSunStatisticsVO.getUpperId().equals(risingSunStatisticsVO.getSecondId())
                            && spaceRisingSunStatisticsVO.getFirstId().equals(risingSunStatisticsVO.getFirstId())) {
                        child.add(risingSunStatisticsVO);
                    }
                }
            }
            spaceRisingSunStatisticsVO.setChildren(child);
        }

        return listUpper;
    }
}
